package net.sea.setup.ui.wizard.impl;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import net.sea.commons.constants.AppConstants;
import net.sea.commons.context.AppContext;
import net.sea.commons.util.StringUtils;
import net.sea.setup.config.ModuleNode;
import net.sea.setup.config.custom.CustomConfig;
import net.sea.setup.ui.wizard.AbstractWizardPage;
import net.sea.setup.ui.wizard.impl.cfg.AbstractCfgPanel;

/**
 * 
 * 配置管理页面
 *
 * @author huachengwu 
 */
@SuppressWarnings("unchecked")
public class CfgManagerPage extends AbstractWizardPage {
	/**
	 * 是否需要数据库配置
	 */
	private static final String DB_NEEDED = "1";
	private JPanel panel = new JPanel();
	private List<AbstractCfgPanel> cfgPanels = new ArrayList<AbstractCfgPanel>();
	private Map<String, AbstractCfgPanel> cfgPanelMap = new HashMap<String, AbstractCfgPanel>();

	public CfgManagerPage() {
		setBorder("配置管理");
		setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBorder(null);
		add(scrollPane);

		scrollPane.setViewportView(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		// JPanel serverCfgPanel = new ServerCfgPanel();
		// add(serverCfgPanel);
		//
		// JPanel clientCfgPanel = new ClientCfgPanel();
		// add(clientCfgPanel);
		//
		// JPanel emCfgPanel = new EMCfgPanel();
		// add(emCfgPanel);
	}

	private static final long serialVersionUID = -7572574135317962728L;

	@Override
	public void ready() {
		super.ready();
		cfgPanels.clear();
		panel.removeAll();
		List<ModuleNode> modules = (List<ModuleNode>) AppContext.getInstance()
				.getAttribute(AppConstants.SELECTED_MODULES);
		if (modules == null) {
			logger.error("未选择安装组件");
			throw new RuntimeException("未选择安装组件");
		}
		boolean isNeededDbCfg = false;
		for (ModuleNode moduleNode : modules) {
			String cfgRequired = moduleNode.getCfgRequired();
			if (StringUtils.isNotEmpty(cfgRequired)) {
				try {
					AbstractCfgPanel cfgPanel = cfgPanelMap.get(cfgRequired);
					if (cfgPanel == null) {
						cfgPanel = (AbstractCfgPanel) (Class
								.forName(cfgRequired).newInstance());
					}
					cfgPanels.add(cfgPanel);
					panel.add(cfgPanel);
				} catch (Exception e) {
					logger.error("配置页面加载异常", e);
					throw new RuntimeException(e);
				}
			}
			isNeededDbCfg = isNeededDbCfg
					|| DB_NEEDED.equals(moduleNode.getDbRequired());
		}
		contextInst.setAttribute(AppConstants.DB_CFG_FLAG, isNeededDbCfg);
	}

	@Override
	public PageResult validateInput() {
		for (AbstractCfgPanel cfgPanel : cfgPanels) {
			PageResult pageResult = cfgPanel.validateInput();
			if (!pageResult.isPassed()) {
				return pageResult;
			}
		}
		return new PageResult(true);
	}

	@Override
	protected List<CustomConfig> createCustomConfig() {
		List<CustomConfig> configs = new ArrayList<CustomConfig>();
		for (AbstractCfgPanel cfgPanel : cfgPanels) {
			configs.add(cfgPanel.buildCfg());
		}
		return configs;
	}
}
